package com.sm.appbase.net.base;

import android.util.Log;

import com.google.gson.JsonParseException;
import com.sm.appbase.base.basemvp.BasePresenter;
import com.sm.appbase.base.basemvp.BaseView;
import com.sm.appbase.net.exception.NetErrorException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public abstract class ApiSubscriber<T> implements Observer<T> {
    private BaseView view;
    private BasePresenter presenter;
    private boolean isShowLoading = true;

    public ApiSubscriber(BaseView view, BasePresenter presenter) {
        this.view = view;
        this.presenter = presenter;
    }

    public ApiSubscriber(BaseView view, BasePresenter presenter, boolean isShowLoading) {
        this.view = view;
        this.presenter = presenter;
        this.isShowLoading = isShowLoading;
    }

    @Override
    public void onSubscribe(Disposable d) {
        view.bindDispos(d);
        if ((presenter.isViewAttached()) && view != null && isShowLoading) {
            view.showLoading();
        }
    }

    @Override
    public void onError(Throwable e) {
        Log.e("Subsriber", "onError");
        Log.e("Subsriber", e.getMessage());

        NetErrorException error = null;
        if (e != null) {
            // 对不是自定义抛出的错误进行解析
            if (!(e instanceof NetErrorException)) {
                if (e instanceof UnknownHostException) {
                    error = new NetErrorException(e, NetErrorException.NoConnectError);
                } else if (e instanceof JSONException || e instanceof JsonParseException) {
                    error = new NetErrorException(e, NetErrorException.PARSE_ERROR);
                } else if (e instanceof SocketTimeoutException) {
                    error = new NetErrorException(e, NetErrorException.SocketTimeoutError);
                } else if (e instanceof ConnectException) {
                    error = new NetErrorException(e, NetErrorException.ConnectExceptionError);
                } else {
                    error = new NetErrorException(e, NetErrorException.OTHER);
                }
            } else {
                error = (NetErrorException) e;
            }
        }

        // 回调抽象方法
        onFail(error);

    }

    @Override
    public void onNext(T t) {
        if ((presenter.isViewAttached()) && view != null) {
            view.hideLoading();
        }
    }

    /**
     * 回调错误
     */
    protected void onFail(NetErrorException error) {
        if (isShowLoading) {
            if (error.getErrorType() != 401)
                view.toast(error.getMessage());
        }
        if ((presenter.isViewAttached()) && view != null) {
            view.hideLoading();
            view.onError(error);
        }

    }

    @Override
    public void onComplete() {
        Log.e("Subsriber", "onComplete");
        if ((presenter.isViewAttached()) && view != null) {
            view.hideLoading();
        }
    }
}
